package com.captjack.dubbo.web.hello.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.captjack.dubbo.api.hello.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Enumeration;

/**
 * @author Capt Jack
 * @date 2017/11/6
 */
@RestController
@RequestMapping(value = "/hello", method = {RequestMethod.GET, RequestMethod.POST})
public class HelloController extends BaseController {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    /**
     *
     */
    private final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    /**
     * 测试service
     */
    @Reference
    private HelloService helloService;

    /**
     * 测试
     */
    @RequestMapping(value = "/hello1")
    public void hello1() {
        try {
            HttpServletRequest request = getRequest();
            Enumeration<String> headers = request.getHeaderNames();
            while (headers.hasMoreElements()) {
                String headerKey = headers.nextElement();
                System.out.println(headerKey + "\t---------\t" + request.getHeader(headerKey));
            }
            writeStringToResponse("{\"msg\":\"成功！\"}");
        } catch (Exception e) {
            logger.error("hello error! cause by : ", e);
        }
    }

    /**
     * 测试
     */
    @RequestMapping(value = "/hello")
    public String hello() {
        String result = null;
        try {
            result = helloService.hello("123456") + SIMPLE_DATE_FORMAT.format(System.currentTimeMillis());
        } catch (Exception e) {
            logger.error("hello error! cause by : ", e);
        }
        return result;
    }

    @Override
    protected void service() {

    }

}